package com.orangelife.mobile.complaints.biz;

import android.os.Handler;

import com.common.sp.CommSp2;
import com.curry.android.callback.ICallBack;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.JSONHelper;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.common.interfases.Request;
import com.orangelife.mobile.constants.Constant;

import java.util.HashMap;
import java.util.Map;

public class Complaints implements Request {

    private final String TAG = Complaints.class.getSimpleName();

    private static Complaints complaints = null;

    /** 单例 */
    public static Complaints getInstance() {
        if (complaints == null) {
            complaints = new Complaints();
        }
        return complaints;
    }

    /**
     * 添加投诉建议
     *
     * @param map
     * @param handler
     */
    public void addComplainSuggestion(Map<String, Object> map, String filenames, Handler handler) {
        map.put("pics", filenames);
        Map<String, Object> rootMap = new HashMap<>();
        rootMap.put("map", map);
        rootMap.put("url", Constant.URL_COMPLAIN_ADD_NEW);
        rootMap.put("wat", 1);
        JSONRequest.getInstance().sendRequest(JSONRequest.POST, rootMap, handler, HttpUtil.ACTION_JSON);
    }

    public void getMyHousesList(Map<String, Object> map, Handler handler) {
        Map<String, Object> rootMap = new HashMap<>();
        rootMap.put("map", map);
        rootMap.put("url", Constant.URL_MY_HOUSE_LIST);
        rootMap.put("wat", 2);
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
    }

    /**
     * 获取我的投诉列表
     *
     * @param map
     * @param handler
     */
    public void getMyComplainList(Map<String, Object> map, int pageSize, int pageNo, Handler handler) {
        map.put("pageIndex", pageNo);
        map.put("pageSize", pageSize);
        Map<String, Object> rootMap = new HashMap<String, Object>();
        rootMap.put("map", map);
        rootMap.put("url", Constant.URL_COMPLAIN_LIST_NEW);
        rootMap.put("wat", 1);
        JSONRequest.getInstance().sendRequest(JSONRequest.POST, rootMap, handler, HttpUtil.ACTION_JSON);
    }

    /**
     * 获取投诉建议详情
     *
     * @param map
     * @param handler
     */
    public void getComplainDetailInfo(Map<String, Object> map, Handler handler) {
        Map<String, Object> rootMap = new HashMap<String, Object>();
        rootMap.put("map", map);
        rootMap.put("url", Constant.URL_COMPLAIN_DETAIL_NEW);
        rootMap.put("wat", 2);
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
    }

    /**
     * 添加评价
     *
     * @param map
     * @param handler
     */
    public void addJudgement(Map<String, Object> map, Handler handler) {
        Map<String, Object> rootMap = new HashMap<String, Object>();
        rootMap.put("map", map);
        rootMap.put("url", Constant.URL_JUDGEMENT_ADD);
        rootMap.put("wat", 1);
        JSONRequest.getInstance().sendRequest(JSONRequest.PUT, rootMap, handler, HttpUtil.ACTION_JSON);
    }

    // ----------------------------------- 底下这些是以前的东西，不知道还会不会用到，就没删了 ---------------------------------------------

    /** 提交 */
    @Override
    public void submit(Object obj, Handler handler) throws Exception {
        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) obj;
        HttpUtil.getHttp().post(Constant.URL_COMPLAINTS, map, 2, handler, new ICallBack() {

                    @Override
                    public void request(String buf, Handler handler, int wat)
                            throws Exception {
                        handler.obtainMessage(wat, JSONHelper.jsonToMap(buf).get("errInfo").toString().equals("OK") ? "保存成功" : "保存失败").sendToTarget();
                    }

                }
                , HttpUtil.ACTION_JSON);
    }

    /** 查询 */
    @Override
    public void select(Object obj, Handler handler) throws Exception {
        Map<String, Object> map = (Map<String, Object>) obj;
        HttpUtil.getHttp().get(Constant.URL_COMPLAINTS + CommSp2.getCommID() + "/ComplaintSuggestion?", map, 1, handler, new ICallBack() {

                    @Override
                    public void request(String buf, Handler handler, int wat)
                            throws Exception {
                        handler.obtainMessage(wat, buf).sendToTarget();
                    }

                }
                , HttpUtil.ACTION_URI);
    }

    /** 判断是否有正在进行的业主验证 */
    public void isAccepValidate(Object obj, Handler handler) throws Exception {
        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) obj;
        HttpUtil.getHttp().get(Constant.URL_OWNER_VALIDATE_ISACCEPT, null, 3, handler, new ICallBack() {

                    @Override
                    public void request(String buf, Handler handler, int wat)
                            throws Exception {
                        handler.obtainMessage(wat, JSONHelper.jsonToMap(buf).get("isCheChing").toString().equals("false") ? "无验证" : "正在验证").sendToTarget();
                    }

                }
                , HttpUtil.ACTION_URI);
    }


}
